package GUI;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.WindowConstants;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class MasterGUI extends JFrame implements ActionListener  {
	
	private static final long serialVersionUID = 1L;
	private JButton btnLogOut;
	private MainGUI maingui;
	private JButton btnMainMenu;
	private JPanel panel;
	
	
	private void intialize(){ 
		this.setSize(900, 640);
		this.setTitle("ICM");
	}
	
	public MasterGUI(final MainGUI maingui) {
		//setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		
		intialize(); 
		this.maingui=maingui;
		getContentPane().setLayout(null);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				maingui.logoutHandler();
				try {
					maingui.clientHandler();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
	        	close();
				System.exit(0);
			}
		}); 
		//final JFrame frame = new JFrame(); 
		panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel.setBounds(0, 517, 131, 85);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		btnLogOut = new JButton("Log out");
		btnLogOut.addActionListener(this);
		btnLogOut.setBounds(10, 45, 103, 23);
		panel.add(btnLogOut);
		
		btnMainMenu = new JButton("Main menu");
		btnMainMenu.addActionListener(this);
		btnMainMenu.setBounds(10, 11, 103, 23);
		panel.add(btnMainMenu);
	
	
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnLogOut) {
			JOptionPane.showMessageDialog(serverGUI.mainframe,"button logout pressed");
			maingui.logoutHandler();
			try {
				maingui.clientHandler();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			close();				
				
		}
		
		if (e.getSource()== btnMainMenu){
			JOptionPane.showMessageDialog(serverGUI.mainframe,"button main menu pressed");
			maingui.handleLogin(maingui.usr.getPermissionLvl());
			//setVisible(false);
			close();
		}
		
	}

	public void close(){ // Let us close a window
	  setVisible(false);
		 //WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
		//Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
	} 

	public MainGUI getMaingui() {
		return maingui;
	}

	public void setMaingui(MainGUI maingui) {
		this.maingui = maingui;
	}

}
		
	

